﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using One.Web.UI.WebControls;
using One.Web;
using Money.Data;
using Money.Interfaces;
using One.Framework;
using Resources;

public partial class Admin_EditCurrency : System.Web.UI.Page
{
    #region fields

    private ICurrencyService currencyService;
    private int currencyId;
    private Currency currency;
    private int success;
    private string redirectUrl = "CurrencyManagement.aspx";

    #endregion

    #region events

    protected void Page_Load(object sender, EventArgs e)
    {
        currencyService = ServiceFactory.Create<ICurrencyService>();

        GetParams();
        LoadData();
        AddRules();
        if (!IsPostBack)
        {
            UpdateView();
        }
    }

    protected void AddOrUpdate(object sender, EventArgs e)
    {
        if (validationManager.Validate())
        {
            bool isAdd = false;
            if (currency == null)
            {
                isAdd = true;
                currency = new Currency();
            }
            currency.Name = tbName.Text;
            currency.Description = tbDescription.Text;
            currency.Rate = decimal.Parse(tbRate.Text);

            if (isAdd)
            {
                currencyService.CreateCurrency(currency);
                success = 1;
            }
            else
            {
                currencyService.UpdateCurrency(currency);
                success = 2;
            }
            Response.Redirect(string.Format("{0}?success={1}", redirectUrl, success));
        }
    }

    #endregion

    #region private functions

    private void GetParams()
    {
        currencyId = QueryStringUtility.GetInt32("currencyId", 0);
    }

    private void LoadData()
    {
        try
        {
            if (currencyId != 0)
            {
                currency = currencyService.GetCurrencyById(currencyId);
            }
        }
        catch
        {
            currency = null;
        }
    }

    private void UpdateView()
    {
        (Master as Admin_MasterPage).SelectedTab = "currencyManagement";
        if (currency == null)
        {
            pageTitle.Text = Resources.Strings.AddCurrency;
            btAddOrUpdate.Text = Resources.Strings.Add;
        }
        else
        {
            pageTitle.Text = Resources.Strings.UpdateCurrency;
            btAddOrUpdate.Text = Resources.Strings.Update;
            tbName.Text = currency.Name;
            tbDescription.Text = currency.Description;
            tbRate.Text = currency.Rate.ToString();
        }
    }

  

    private void AddRules()
    {
        validationManager.AddRule(new ValueRequiredRule(tbName, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new ValueRequiredRule(tbRate, Strings.RequiredFieldErrorMessage, Strings.RequiredFieldHint));
        validationManager.AddRule(new DecimalValueRule(tbRate, Strings.DecimalValueErrorMessage, Strings.DecimalValueHint));
        validationManager.AddRule(new CustomeValidatorRule(tbRate, Strings.ValueRangeErrorMessage, Strings.ValueRangeErrorHint, ValidBalance));

        validationManager.Notifier = new BalloonNotifier();
    }

    private void ValidBalance(string strValue, out bool isValid)
    {
        try
        {
            decimal value = decimal.Parse(strValue);
            isValid = value >= 0;
        }
        catch
        {
            isValid = false;
        }
    }


    #endregion

    #region public functions


    #endregion
}
